﻿/*
 * 文件名称：
 * 程序员：yangsiyao
 * 创建时间：2009/09/08
 * 
 * 功能说明：栏目管理
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Config_AddType : System.Web.UI.Page
{
    protected string Action = "";
    protected int ColumnID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断
        Action = NetRequest.GetQueryString("Action");
        ColumnID = NetRequest.GetQueryInt("ColumnID");

        if (Action == "Modify")
        {
            btnUpdate.Text = "修改";
        }
        else if (Action == "Add")
        {
            btnUpdate.Text = "增加";
        }
        else if (Action == "Delete")
        {
            btnAction();
            return;
            //btnUpdate.Text = "删除";
        }

        if (!IsPostBack)
        {
            BindTreeDrop("");

            if (Action == "Modify" || Action == "Delete")
            {
                BindData();
            }
        }
    }
    protected void BindData()
    {
        Whir.Model.Whir_CommonCategory model = new Whir.SQLServerDAL.Whir_CommonCategory().GetModel(ColumnID);
        if (model == null)
            return;

        int parentID = GetAllParentID(model.CategoryId);

        Whir.Model.Whir_CommonCategory parentModel = new Whir.BLL.Whir_CommonCategory().GetModel(parentID);

        this.ddlParent.DataValueField = "CategoryId";
        this.ddlParent.DataTextField = "CategoryName";
        Whir.Common.BindHelper.BindDropDownList(this.ddlParent, "Whir_CommonCategory", parentID.ToString(), " AND CategoryId <> " + model.CategoryId, parentModel.CategoryName);

        //BindTreeDrop(" and CategoryId<>" + ColumnID);

        this.ddlParent.SelectedValue = model.ParentId.ToString();
        txtColumnName.Text = model.CategoryName;
        //txtSort.Text = model.Sort.ToString();
    }

    /// <summary>
    /// 绑定栏目树型下拉列表
    /// </summary>    
    protected void BindTreeDrop(string str)
    {
        Whir.Model.Whir_CommonCategory model = new Whir.SQLServerDAL.Whir_CommonCategory().GetModel(ColumnID);
        if (model != null)
        {

            int parentID = GetAllParentID(model.CategoryId);
            Whir.Model.Whir_CommonCategory parentModel = new Whir.BLL.Whir_CommonCategory().GetModel(parentID);

            this.ddlParent.DataValueField = "CategoryId";
            this.ddlParent.DataTextField = "CategoryName";
            Whir.Common.BindHelper.BindDropDownList(this.ddlParent, "Whir_CommonCategory", parentID.ToString(), "", parentModel.CategoryName);
        }
        else
        {
            this.ddlParent.DataValueField = "CategoryId";
            this.ddlParent.DataTextField = "CategoryName";
            Whir.Common.BindHelper.BindDropDownList(this.ddlParent, "Whir_CommonCategory", "0", str, "-- 类别管理 --");
        }
    }

    private void DeleteColumn()
    {
        Whir.SQLServerDAL.Whir_CommonCategory bll = new Whir.SQLServerDAL.Whir_CommonCategory();
        Whir.Model.Whir_CommonCategory model = bll.GetModel(ColumnID);
        if (model != null)
        {
            bll.Delete(ColumnID);
        }

        DeleteByColumnID(ColumnID);
    }

    /// <summary>
    /// 删除子类栏目
    /// </summary>
    /// <param name="columnID"></param>
    private void DeleteByColumnID(int columnID)
    {
        string ids = GetChildID(columnID.ToString());

        if (ids != string.Empty)
        {
            string strSQL = String.Format("DELETE FROM Whir_CommonCategory WHERE CategoryId IN ({0})", ids.TrimStart(',').TrimEnd(','));
            Whir.SQLServerDAL.DbHelperSQL.ExecuteSql(strSQL);
        }
    }

    /// <summary>
    /// 获得所有子类的编号
    /// </summary>
    private string GetChildID(string parentID)
    {
        string strResult = "";
        if (parentID == "") return strResult;

        string strSQL = String.Format("SELECT CategoryId FROM Whir_CommonCategory WHERE ParentId = {0}", parentID);
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL).Tables[0];

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            strResult += GetChildID(dt.Rows[i]["CategoryId"].ToString()) + ",";
            strResult += dt.Rows[i]["CategoryId"].ToString();
        }

        return strResult;
    }


    /// <summary>
    /// 修改类别内容
    /// </summary>
    private void ModifyColumn()
    {
        Whir.SQLServerDAL.Whir_CommonCategory bll = new Whir.SQLServerDAL.Whir_CommonCategory();
        Whir.Model.Whir_CommonCategory model = bll.GetModel(ColumnID);

        model.ParentId = int.Parse(this.ddlParent.SelectedValue);
        model.CategoryName = txtColumnName.Text;
        //model.Sort = int.Parse(txtSort.Text);

        bll.Update(model);
    }


    /// <summary>
    /// 添加类别
    /// </summary>
    /// <returns>返回添加类别的编号</returns>
    private int AddColumn()
    {
        Whir.Model.Whir_CommonCategory model = new Whir.Model.Whir_CommonCategory();

        model.ParentId = int.Parse(this.ddlParent.SelectedValue);
        model.CategoryName = txtColumnName.Text;
        model.CreateDate = DateTime.Now;
        model.CreateUserID = 0;
        model.Sort = GetMaxSort(model.ParentId); //int.Parse(txtSort.Text);
        model.UpdateDate = DateTime.Now;
        model.UpdateUserID = 0;

        int cloumnID = new Whir.SQLServerDAL.Whir_CommonCategory().Add(model);

        return cloumnID;
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        btnAction();
    }

    private void btnAction()
    {
        int columnID = 0;
        switch (Action)
        {
            case "Add":
                columnID = AddColumn();
                Response.Clear();
                Response.Write("<script>parent.location='CommonCategory.aspx?op=" + Action + "';</script>");
                break;
            case "Modify":
                ModifyColumn();
                Response.Clear();
                Response.Write("<script>parent.location='CommonCategory.aspx?op=" + Action + "';</script>");
                break;
            case "Delete":
                DeleteColumn();
                Response.Redirect("CommonCategory.aspx?op=" + Action, true);
                break;
        }
        Response.End();
        return;
    }

    //protected void btnReset_Click(object sender, EventArgs e)
    //{
    //    Response.Clear();
    //    Response.Write("<script>parent.document.getElementById('btnAdd').style.display='';</script>");
    //    Response.End();
    //    return;
    //}

    /// <summary>
    /// 获取该父级类别的最大记录数,已自动+1
    /// </summary>
    /// <param name="ParentId">父级类别编号</param>
    private int GetMaxSort(int ParentId)
    {
        string strSQL = String.Format("SELECT COUNT(1) + 1 FROM Whir_CommonCategory WHERE ParentId = {0}", ParentId);
        object obj = Whir.SQLServerDAL.DbHelperSQL.GetSingle(strSQL);

        return obj != null ? Convert.ToInt32(obj) : 0;
    }

    private int GetAllParentID(int categoryID)
    {
        int parentID = 0;
        Whir.Model.Whir_CommonCategory model = new Whir.SQLServerDAL.Whir_CommonCategory().GetModel(categoryID);
        if (model != null)
        {
            if (model.ParentId != 0)
                parentID = GetAllParentID(model.ParentId);
            else
                parentID = categoryID;
        }

        return parentID;
    }
}
